/**
* Copyright (C) 2018-2022
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.rest;

import co.yixiang.domain.PageResult;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.logging.aop.log.Log;
import co.yixiang.modules.order.param.BackstageCreateOrderParam;
import co.yixiang.modules.shop.param.ProductInputFormParam;
import co.yixiang.modules.shop.service.YxStoreProductStockRecordService;
import co.yixiang.modules.shop.service.dto.YxStoreProductStockRecordDto;
import co.yixiang.modules.shop.service.dto.YxStoreProductStockRecordQueryCriteria;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @author hdb
* @date 2024-05-07
*/
@AllArgsConstructor
@Api(tags = "商品库存出入库记录表管理")
@RestController
@RequestMapping("/api/yxStoreProductStockRecord")
public class YxStoreProductStockRecordController {

    private final YxStoreProductStockRecordService yxStoreProductStockRecordService;
    private final IGenerator generator;


    @Log("商品库存出入库记录导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxStoreProductStockRecord:list')")
    public void download(HttpServletResponse response, YxStoreProductStockRecordQueryCriteria criteria,Integer type) throws IOException {
        yxStoreProductStockRecordService.download(generator.convert(yxStoreProductStockRecordService.queryAll(criteria), YxStoreProductStockRecordDto.class), response,type);
    }

    @GetMapping
    @Log("查询商品库存出入库记录表")
    @ApiOperation("查询商品库存出入库记录表")
    @PreAuthorize("@el.check('admin','yxStoreProductStockRecord:list')")
    public ResponseEntity<PageResult<YxStoreProductStockRecordDto>> getYxStoreProductStockRecords(YxStoreProductStockRecordQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreProductStockRecordService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping(value = "/queryRecordListByRecordSn")
    @ApiOperation("根据销售记录单号查询记录")
    @PreAuthorize("@el.check('admin','yxStoreProductStockRecord:list')")
    public ResponseEntity<List<Map<String, Object>> > queryRecordListByRecordSn(@RequestBody JSONObject paramObject){
        return new ResponseEntity<>(yxStoreProductStockRecordService.queryRecordListByRecordSn(paramObject),HttpStatus.OK);
    }

    /**
     * 后台采购入库，商品采购入库
     * @param resources
     * @return
     */
    @ApiOperation(value = "后台采购入库，商品采购入库")
    @PostMapping("/productInputForm")
    @PreAuthorize("@el.check('admin','yxStoreProductStockRecord:list')")
    public ResponseEntity productInputForm(@Validated @RequestBody ProductInputFormParam resources) {
        yxStoreProductStockRecordService.productInputForm(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
}
